package com.woniu.controller;

import com.woniu.entity.User;
import com.woniu.service.UserService;
import com.woniu.utils.MinioUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user2")
//@CrossOrigin
public class User2Controller {
    @Resource
    private UserService userService;
    @Resource
    private MinioUtil minioUtil;
    @GetMapping
    public List<User> findAll(){

        return userService.findAll();
    }
    @GetMapping("/{id}")
    public User findById(@PathVariable Integer id){
        return userService.findById(id);
    }
//    @PostMapping
//    public Integer add(@RequestBody User user){
//        return userService.add(user);
//    }
    @PostMapping
    public Map<String,Object> login(@RequestBody User user){
        return userService.login(user);
    }
//    @PutMapping
//    public Integer Update(User user){
//        return userService.update(user);
//    }
    @DeleteMapping("/{id}")
    public Map<String,Object> delete(@PathVariable Integer id) {
        Integer delete = userService.delete(id);
        HashMap<String, Object> map = new HashMap<>();
        if (delete>0) {
            map.put("code",200);
            map.put("msg","删除成功");
        }else {
            map.put("code",500);
            map.put("msg","删除失败");
        }
        return map;
    }
    @PostMapping("/add")
    public Map<String,Object> add(@RequestBody User user){
        Integer result = userService.add(user);
        HashMap<String, Object> map = new HashMap<>();
        if (result>0) {
            map.put("code",200);
            map.put("msg","添加成功");
        }else {
            map.put("code",500);
            map.put("msg","添加失败");
        }
        return map;
    }
    @PostMapping("/upload")
    public Map<String,Object> upload(MultipartFile file) throws Exception {
        Map<String, Object> map = minioUtil.updateFile(file, "data");
        return map;
    }
}
